import org.springframework.cglib.proxy.Enhancer;
import org.springframework.cglib.proxy.MethodInterceptor;
import org.springframework.cglib.proxy.MethodProxy;

import java.lang.reflect.Method;
import java.lang.reflect.Proxy;


/**
 * 测试运行时 常量池（在方法区中method area）的内存溢出
 * VM args: -XX:PermSize=10M -XX:MaxPermSize=10M  //java 8已经弃用
 * VM args: -XX:MetaspaceSize=10M -XX:MaxMetaspaceSize=10M
 */
public class RuntimeConstantPoolOutOfMemory {
	public static void main(final String[] args) {
		while (true) {
			Enhancer enhancer = new Enhancer();
			enhancer.setSuperclass(OOMObject.class);
			enhancer.setUseCache(false);
			enhancer.setCallback(new MethodInterceptor() {
				@Override
				public Object intercept(Object o, Method method, Object[] objects, MethodProxy methodProxy) throws Throwable {
					return methodProxy.invoke(o, args);
				}
			});
			enhancer.create(); //Exception in thread "main" java.lang.OutOfMemoryError: Metaspace
		}

	}

}
